package yt.kpool.activity.setting.agent.myguest;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import yt.kpool.R;
import yt.kpool.activity.setting.agent.recommendcustomers.RecommendCustomersAc;
import yt.kpool.adapter.MinerLayoutSelectListviewAdapter;
import yt.kpool.adapter.ModeSelectListviewAdapter;
import yt.kpool.bean.BaseBean;
import yt.kpool.bean.CommentItem;
import yt.kpool.bean.CurrencyList;
import yt.kpool.bean.RecommendCustomersBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.util.imageUtil.GlideUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;

/**
 * 分层模式 选择
 */
public class HierarchicalModeActivity extends AppCompatActivity {
    private LoadingDialog loadingDialog;
    @BindView(R.id.lv_select)
    ListView lv_select;
    private ModeSelectListviewAdapter  modeSelectListviewAdapter;
    private List<RecommendCustomersBean.DataBean> list;

    /**
     *
     * @param mactivity
     * @param ID
     * @param type  客户4 代理3
     */
    public static void start(Activity mactivity, int ID,int type) {
        Intent intent = new Intent(mactivity, HierarchicalModeActivity.class);
        intent.putExtra("ID", ID);
        intent.putExtra("type", type);
        mactivity.startActivityForResult(intent,12);
    }



    public Integer getID() {
        return getIntent().getIntExtra("ID",0);
    }
    public Integer gettype() {
        return getIntent().getIntExtra("type",0);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hierarchical_mode);
        ButterKnife.bind(this);
        initTitle();
        initlist();
        getdata();
    }

    private void getdata() {

        if (NetWorkUtil.check(this)) {
            loadingDialog.show();

            NetHelperNew.PromotesList(new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "分层模式：" + data);
                    RecommendCustomersBean recommendCustomersBean = MyGson.getInstance().fromJson(data, RecommendCustomersBean.class);
                    if (recommendCustomersBean.getType() == 1) {
                        if (recommendCustomersBean.getData().size() > 0) {

                            for (int i = 0; i < recommendCustomersBean.getData().size(); i++) {

                                //判断筛选type
                                if (gettype()==recommendCustomersBean.getData().get(i).getType())
                                {
                                    list.add(recommendCustomersBean.getData().get(i));
                                }
                            }

                            modeSelectListviewAdapter.notifyDataSetChanged();
                            modeSelectListviewAdapter.setSelected(getID());
                        } else {
                            ToastUtil.showToast(HierarchicalModeActivity.this, "没有可查询数据");
                        }
                    }else {

                        new ErrorDialog(HierarchicalModeActivity.this, recommendCustomersBean.getContent()).show();
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(HierarchicalModeActivity.this, msg);
                }
            });


        }

    }


    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("选择分成模式");
        titleControl.setRightText("保存", new TitleControl.OnClickRight() {
            @Override
            public void onRight() {
               modifyUserProportion();
            }
        });
        loadingDialog = new LoadingDialog(this, 0);
    }

    /**
     * 修改客户分成模式
     */
    private void modifyUserProportion() {
        if (NetWorkUtil.check(this)) {
            loadingDialog.show();

            NetHelperNew.ModifyUserProportion(modeSelectListviewAdapter.getselectedId()+"",getID()+"",new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "修改分层模式：" + data);
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                    if (baseBean.getType() == 1) {
                        ToastUtil.showToast(HierarchicalModeActivity.this, baseBean.getContent());
                        setResult(32);
                        finish();

                    }else {

                        new ErrorDialog(HierarchicalModeActivity.this, baseBean.getContent()).show();
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);
                    loadingDialog.dismiss();

                    ToastUtil.showToast(HierarchicalModeActivity.this, msg);
                }
            });


        }

    }

    private void initlist() {
        list=new ArrayList<>();
        modeSelectListviewAdapter = new ModeSelectListviewAdapter(this, list);
        lv_select.setAdapter(modeSelectListviewAdapter);

        lv_select.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                if (list.size()>i)
                {
                    modeSelectListviewAdapter.setSelected(list.get(i).getProportionID());
                }



            }
        });
    }
}
